import struct
import time
import json
import http.client
import urllib
import HttpPost
import Encoder
import Teminal
import DataAnalyze
import datetime
import os
import Encoder
import DataAnalyze
import TimeGet


def file_write(data_str, path, make_path=None):
	if not isinstance(data_str, (str)):
		raise TypeError('bad operand type, parameter must be a string')
	timeStr = TimeGet.get_time_str()
	if make_path != None:
		if os.path.exists("./%s/%s" %(path,make_path)) == False:
			os.makedirs("./%s/%s/" %(path, make_path))
		fileStr = timeStr.split(':')[0]+'.txt'	
		data_path = "./%s/%s/%s" %(path,make_path,fileStr)
	else:
		fileStr = timeStr.split(':')[0]+'.txt'	
		data_path = "./%s/%s" %(path,fileStr)
	fp = open(data_path, 'a')
	fp.write(data_str)
	fp.close()
	return True

def main():
	t = (0x0A,0x06, 0x91, 0x04, 0x78, 0x78, 0x41, 0x01, 0x0B,
	    0x0A, 0x06, 0x12, 0x04, 0x78, 0x21, 0x41, 0x01, 0x0B,  
	    0x0A, 0x06, 0x13, 0x04, 0x78, 0x22, 0x41, 0x02, 0x0B,
	    0x0A, 0x06, 0x14, 0x04, 0x78, 0x24, 0x41, 0x03, 0x0B,
	    0x0A, 0x06, 0x14, 0x04, 0x78, 0x24, 0x41, 0x03, 0x0B,
	    0x0A, 0x06, 0x14, 0x04, 0x78, 0x24, 0x41, 0x03, 0x0B,
	    0x0A, 0x06, 0x14, 0x04, 0x78, 0x24, 0x41, 0x03, 0x0B,
	    0x0A, 0x06, 0x14, 0x04, 0x78, 0x24, 0x41, 0x03, 0x0B,
	    0x0A, 0x06, 0x14, 0x04, 0x78, 0x24, 0x41, 0x03, 0x0B,
	    0x0A, 0x06, 0x14, 0x04, 0x78, 0x24, 0x41, 0x03, 0x0B,
	    0x0B, 0x0A, 0xFE)
	if DataAnalyze.data_correct(t, 9):
		list_data = DataAnalyze.data_analyze(t, 9)
		file_write(list_data)

		



if __name__ == '__main__':
	main()

